package com.szeastroc.controller.system;


import com.github.pagehelper.PageInfo;
import com.szeastroc.base.controller.BaseController;
import com.szeastroc.bean.system.SystemResource;
import com.szeastroc.bean.system.SystemRole;
import com.szeastroc.bean.system.SystemUser;
import com.szeastroc.common.Constant;
import com.szeastroc.service.system.SystemResourceService;
import com.szeastroc.service.system.SystemRoleService;
import com.szeastroc.service.system.SystemUserService;
import com.szeastroc.utils.LayerTableDataUtil;
import com.szeastroc.utils.Result4Ajax;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/systemUser")
public class SystemUserController extends BaseController{
	
	@Autowired
    SystemUserService userService;

    @Autowired
    SystemResourceService systemResourceService;

    @Autowired
    SystemRoleService systemRoleService;

    @RequestMapping("/login")
    public String login(HttpServletRequest request, String username, String passWord,String remberMe,Model model){
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(passWord)){
            model.addAttribute("error", "用户名或密码不能为空");
            return "index/index.html";
        }
        SystemUser user = userService.selectByUserName(username);
        if(user == null || !passWord.equals(user.getPassword())){
            model.addAttribute("error", "用户名或密码错误!");
            model.addAttribute("username",username);
            return "index/index.html";
        }

        //获取菜单
        //List<SystemResource> resources = systemResourceService.getSystemResource(user.getId(),user.getRoleId());

        HttpSession session = request.getSession();
        session.setAttribute(Constant.USER_SESSION,user);
        model.addAttribute("user", user);
        //model.addAttribute("resources", resources);
        return "main/main.html";
    }


    @GetMapping("/index")
    public String userIndex(Model model){
        List<SystemRole> roles = systemRoleService.selectAll();
        model.addAttribute("roles",roles);
        return "system/system_user.html";
    }


    @PostMapping("/getUserList")
    @ResponseBody
    public LayerTableDataUtil getUserList(Model model,Integer page,Integer limit){
    	PageInfo<SystemUser> pageinfo = userService.selectPage(page, limit);
        LayerTableDataUtil data = new LayerTableDataUtil(pageinfo.getTotal(),pageinfo.getList());
    	return data;
    }


    @PostMapping("/updateUserRole")
    @ResponseBody
    public Result4Ajax updateUserRole(Integer userId, Integer roleId){
        try {
            userService.updateUserRole(userId, roleId);
            return super.success();
        } catch (Exception e) {
           logger.error("修改用户角色错误!",e);
            return super.error();
        }
    }
}
